package com.zhou.jiancai.Utils;

import com.zhou.jiancai.common.Constants;

import java.io.IOException;
import java.util.IdentityHashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/4/17.
 */

public class OkhttpUtils {


    public static OkHttpClient okHttpClient;
    public static Response response;

    /*get方法*/
    public static Response getHttp(String url) {

        try {
            if (null == okHttpClient) {
                okHttpClient = new OkHttpClient();


            } else {
               /* okHttpClient .connectTimeoutMillis();//可以设置但是得自定义OkHttpClient设置写的超时时间  默认是10*1000
                okHttpClient.readTimeoutMillis();//设置连接超时时间 默认是10*1000
                okHttpClient.writeTimeoutMillis();//设置连接超时时间 默认是10*1000
*/

                Request request = new Request.Builder()
                        .url(url)
                         .get()
                        .build();

                response = okHttpClient.newCall(request).execute();
                LogUtils.i(response.body().toString());

            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.i("okhttp网络请求错误");


        }
        if (response != null) {
            LogUtils.i("okhttp网络请求成功");

            return response;
        } else {
            LogUtils.i("response为null");

            return null;
        }
    }

    /*post方法*/
    public Response postHttp(RequestBody body, String url) {

        try {
            if (null == okHttpClient) {
                okHttpClient = new OkHttpClient();
                okHttpClient.connectTimeoutMillis();//可以设置但是得自定义OkHttpClient设置写的超时时间  默认是10*1000
                okHttpClient.readTimeoutMillis();//设置连接超时时间 默认是10*1000
                okHttpClient.writeTimeoutMillis();//设置连接超时时间 默认是10*1000
            } else {

                Request request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
                // Constants.Newworkinfo  这是get的http
                response = okHttpClient.newCall(request).execute();
                return response;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.i("okhttp网络请求错误");


        }
        if (response != null) {
            LogUtils.i("okhttp网络请求成功");

            return response;
        } else {
            LogUtils.i("response为null");

            return null;
        }
    }
}
